Polymorphism in Java
๐ญ Polymorphism in Java - The Shape-Shifting Superpowerโ
Polymorphism in Java is like a master of disguiseโit allows a class to behave differently in different contexts. It's one of the four pillars of object-oriented programming (OOP), along with inheritance, abstraction, and encapsulation. Think of it as an actor playing different roles in different movies! ๐ฌ
๐ค What is Polymorphism?โ
Polymorphism lets a class act like a chameleon, adapting to different scenarios. Imagine a superhero who wears different costumes but still remains the same person inside! ๐ฆธโโ๏ธ
In Java, we can relate polymorphism to one interface having multiple implementations. The contract remains the same, but each class implements it differently.
For example, a reference variable of a superclass can refer to an instance of a subclass:
Object o = new Object(); // o can hold the reference of any subtype
Object o = new String();
Object o = new Integer();
Here, String
and Integer
are subclasses of Object
. It's like calling someone "Human" whether they are an athlete, a scientist, or a musician! ๐ถ๐โโ๏ธ๐ฌ
๐ Types of Polymorphismโ
Java polymorphism comes in two flavors:
- Compile-time polymorphism (a.k.a. method overloading, static binding)
- Runtime polymorphism (a.k.a. method overriding, dynamic binding)
These types are specific to Java, though polymorphism exists in many forms across different programming languages.
๐๏ธ 1. Compile-Time Polymorphism (Method Overloading)โ
As the name suggests, in compile-time polymorphism, the method to be executed is determined at compile time. It is achieved using method overloading.
โจ How does it work?โ
An object can have multiple methods with the same name, but different:
- Number of parameters
- Parameter types
๐ข Example: Calculator Classโ
public class Calculator {
public Integer sum(Integer a, Integer b) {
return a + b;
}
public Float sum(Float a, Float b) {
return a + b;
}
public Double sum(Double a, Double b) {
return a + b;
}
}
Now, when we invoke sum()
, the compiler decides which method to call based on argument types. It's like a waiter knowing whether you want coffee โ or tea ๐ต based on your order!
Calculator calc = new Calculator();
Integer sum1 = calc.sum(1 ,2);
Float sum2 = calc.sum(1f ,2f);
Double sum3 = calc.sum(1d ,2d);
โณ 2. Runtime Polymorphism (Method Overriding)โ
Runtime polymorphism is all about method overriding. The method that gets executed is determined at runtime, based on the actual instance of the object.
๐พ Example: Animal Kingdom ๐ถ๐ฑโ
Think of an Animal
class with a makeNoise()
method. Dogs ๐ bark, cats ๐ meow, but both are still animals!
class Animal {
public void makeNoise() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
public void makeNoise() {
System.out.println("Bark");
}
}
class Cat extends Animal {
public void makeNoise() {
System.out.println("Meow");
}
}
๐๏ธ Calling the Methodโ
At runtime, Java decides which method to invoke based on the actual instance:
Animal cat = new Cat();
cat.makeNoise(); // Prints "Meow"
Animal dog = new Dog();
dog.makeNoise(); // Prints "Bark"
This is like a remote control! ๐ฎ The button is the same, but whether you control a toy car ๐ or a drone ๐ depends on the actual object!
๐ฏ Conclusionโ
- Polymorphism allows objects, methods, and variables to take multiple forms.
- Java supports method overloading (compile-time) and method overriding (runtime) polymorphism.
- Operator overloading exists in Java only for the
+
operator (e.g., adding numbers vs. concatenating strings).
Polymorphism makes Java flexible, scalable, and powerfulโjust like a superhero with multiple disguises! ๐ฆธโโ๏ธ๐ช
Happy Coding! ๐๐